package com.mmall.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.mmall.common.ServerResponse;
import com.mmall.dao.CategoryMapper;
import com.mmall.pojo.Category;
import com.mmall.service.ICategoryService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;


/**
 * @author:lzq
 * @date: 2018/3/15 16:22
 */
@Service("iCategoryService")
public class CategoryServiceImpl implements ICategoryService{
    private  Logger logger= LoggerFactory.getLogger(CategoryServiceImpl.class);
    @Autowired
    private CategoryMapper categoryMapper;


    public ServerResponse addCategory(Integer parentid,String categoryName){
        if(parentid==null|| StringUtils.isEmpty(categoryName)){
            return ServerResponse.createByErrorMessage("添加品类时参数错误");
        }
        Category category=new Category();
        category.setName(categoryName);
        category.setParentId(parentid);
        category.setStatus(true);//表示分类可用
        int count = categoryMapper.insert(category);
        if(count>0){
            return ServerResponse.createBySuccessMessage("添加品类成功");
        }
        return ServerResponse.createByErrorMessage("添加品类失败");
    }

    public ServerResponse upadteCategory(String categoryName,Integer categoryid){
        if(categoryid==null||StringUtils.isEmpty(categoryName)){
            return ServerResponse.createByErrorMessage("更新品类时参数错误");
        }
        Category category=new Category();
        category.setName(categoryName);
        category.setId(categoryid);
        int count = categoryMapper.updateByPrimaryKeySelective(category);
        if(count>0){
            return  ServerResponse.createBySuccessMessage("品类更新成功");
        }
        return  ServerResponse.createBySuccessMessage("品类更新失败");
    }

    public ServerResponse<List<Category>> getChildParallelCategory(Integer categoryid){
        List<Category> categoryList = categoryMapper.selectCategoryChildrenByParentId(categoryid);
        if(CollectionUtils.isEmpty(categoryList)){
            logger.info("当前分类没有子分类");
        }
        return ServerResponse.createBySuccess(categoryList);
    }

    public ServerResponse<List<Integer>> selectCategoryAndChildrenById(Integer categoryId){
        Set<Category> categories=Sets.newHashSet();
        findChildCategory(categories,categoryId);
        List<Integer> categoryIdList= Lists.newArrayList();
        if(categories!=null){
            for (Category category : categories) {
                categoryIdList.add(category.getId());
            }
        }
        return ServerResponse.createBySuccess(categoryIdList);
    }

    /**
     * d递归算法查询子节点
     * @param categorySet
     * @param categoryId
     * @return
     */
    private Set<Category> findChildCategory(Set<Category> categorySet,Integer categoryId ){
        Category category = categoryMapper.selectByPrimaryKey(categoryId);
        if(category != null){
            categorySet.add(category);
        }
        //递归算法查询节点的子节点
        List<Category> categoryList = categoryMapper.selectCategoryChildrenByParentId(categoryId);
        for (Category categoryItem : categoryList) {
            findChildCategory(categorySet,categoryItem.getId());
        }
        return  categorySet;
    }
}
